<template>
  <div class="login-container">
    <div class="login-box">
      <h2>用户登录</h2>
      <div class="input-item">
        <label>
          <i class="fa fa-user"></i>
          <span>账号：</span>
        </label>
        <input type="text" v-model="keyword" placeholder="请输入用户名或手机号" />
      </div>
      <div class="input-item">
        <label>
          <i class="fa fa-lock"></i>
          <span>密码：</span>
        </label>
        <input type="password" v-model="password" placeholder="请输入密码" />
      </div>
      <button class="login-btn" @click="handleLogin">
        <i class="fa fa-sign-in"></i>
        <span>登录</span>
      </button>
      <div class="register-tip">
        没有账号？<router-link to="/register">去注册</router-link>
      </div>
    </div>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  name: 'LoginView',
  data() {
    return {
      keyword: '',
      password: '',
      errorMsg: ''
    };
  },
  methods: {
    async handleLogin() {
      console.log("关键字"+this.keyword);
      console.log("密码"+this.password);
      // 表单验证
      if (!this.keyword || !this.password) {
        this.errorMsg = '用户名/手机号和密码不能为空';
        alert('登录失败!');
        return;
      }
      await axios.post('/api/user/login?' + "keyword=" + this.keyword + "&password=" + this.password).then(res => {
        console.log(res.data)
        alert('登录成功!');
        sessionStorage.setItem('loginUser', JSON.stringify(res.data));
        this.$router.push('/');
      }).catch(err => {
        console.log(err);
        this.errorMsg = '用户名/手机号或密码错误';
        alert('登录失败!');
      });
    }
  }
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-image: url('../../assets/user/1.jpg');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

.login-box {
  width: 400px;
  padding: 20px;
  background-color: rgba(255, 255, 255, 0.9);
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.input-item {
  margin-bottom: 18px;
  display: flex;          /* 使用Flex布局 */
  align-items: center;    /* 垂直居中 */
}

.input-item label {
  display: flex;          /* 使图标和文字在标签内水平排列 */
  align-items: center;    /* 图标和文字垂直居中 */
  width: 80px;            /* 固定标签宽度 */
  margin-bottom: 0;
  font-weight: 500;
}

.input-item i {
  margin-right: 8px;       /* 图标与文字间距 */
  color: #409eff;         /* 图标颜色与主色调一致 */
}

.input-item span {
  margin-right: 4px;       /* 文字与输入框间距 */
}

.input-item input {
  flex: 1;                /* 输入框占据剩余空间 */
  padding: 10px 12px;      /* 调整内边距，使布局更适中 */
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  font-size: 14px;
  transition: border-color 0.2s;
}

.login-btn {
  display: flex;          /* 使图标和文字在按钮内水平排列 */
  align-items: center;    /* 图标和文字垂直居中 */
  justify-content: center;/* 内容居中 */
  width: 100%;
  padding: 12px 0;        /* 调整内边距，使按钮更协调 */
  background-color: #409eff;
  color: #fff;
  border: none;
  border-radius: 4px;
  font-size: 16px;
  cursor: pointer;
  transition: background-color 0.2s;
}

.login-btn i {
  margin-right: 6px;       /* 按钮图标与文字间距 */
}

.login-btn span {
  font-weight: 500;
}

.register-tip {
  text-align: center;
  margin-top: 15px;
  font-size: 14px;
  color: #606266;
}

.register-tip a {
  color: #409eff;
  text-decoration: none;
}

.register-tip a:hover {
  text-decoration: underline;
}
</style>